package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.QueryTrainingLevelDTO;
import com.woniu.entity.vo.TrainingLevelVO;
import com.woniu.pojo.TrainingLevel;
import com.woniu.mapper.TrainingLevelMapper;
import com.woniu.service.ITrainingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-03-31
 */
@Service
public class TrainingLevelServiceImpl extends ServiceImpl<TrainingLevelMapper, TrainingLevel> implements ITrainingLevelService {
    @Autowired
    private TrainingLevelMapper trainingLevelMapper;
//    分页查询靶场


    @Override
    public PageData listSearch(Integer currentPage, Integer pageSize, QueryTrainingLevelDTO queryTrainingLevelDTO) {
        //生成wapper,并对传回的Dto对象进行校验
        QueryWrapper<TrainingLevel> wrapper = new QueryWrapper();
//        if(queryTrainingLevelDTO != null){
//            if(!StringUtils.isEmpty(queryTrainingLevelDTO.getTrainingId())){
//                wrapper.eq("training_Id", queryTrainingLevelDTO.getTrainingId());
//            }
//            if(!StringUtils.isEmpty(queryTrainingLevelDTO.getTrainingName())){
//                wrapper.like("training_name", queryTrainingLevelDTO.getTrainingName());
//            }
//            if(queryTrainingLevelDTO.getParentId() != null){
//                wrapper.eq("parentId", queryTrainingLevelDTO.getParentId() );
//            }
//            if(queryTrainingLevelDTO.getCreateTimeStart() != null){
//                wrapper.gt("create_time", queryTrainingLevelDTO.getCreateTimeStart());
//            }
//            if(queryTrainingLevelDTO.getCreateTimeEnd() != null){
//                wrapper.lt("create_time", queryTrainingLevelDTO.getCreateTimeEnd());
//            }
//        }
        // 构建分页对象
        IPage<TrainingLevel> page = new Page<>(currentPage, pageSize);
        // 查询
        IPage<TrainingLevel> trainingLevelIPage = trainingLevelMapper.selectPage(page, wrapper);
        // 返回数据
        return new PageData(trainingLevelIPage.getTotal(), trainingLevelIPage.getRecords());
    }

    @Override
    public Boolean nameIsExist(String name) {
        QueryWrapper<TrainingLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("training_name", name);
        TrainingLevel trainingLevel = trainingLevelMapper.selectOne(wrapper);
        if (!StringUtils.isEmpty(trainingLevel)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean idIsExist(Integer id) {
        QueryWrapper<TrainingLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("training_id", id);
        TrainingLevel trainingLevel = trainingLevelMapper.selectOne(wrapper);
        if (!StringUtils.isEmpty(trainingLevel)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public PageData listLevel(Integer currentPage, Integer pageSize, QueryTrainingLevelDTO queryTrainingLevelDTO) {
        // 构建分页对象
        IPage<TrainingLevel> page = new Page<>(currentPage, pageSize);
        // 查询
        IPage<TrainingLevelVO> trainingLevelIPage = trainingLevelMapper.listLevel(page,queryTrainingLevelDTO);
        // 返回数据
        return new PageData(trainingLevelIPage.getTotal(), trainingLevelIPage.getRecords());
    }

    @Override
    public Integer getParentId(String pName) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("training_id").eq("training_name",pName);
        TrainingLevel trainingLevel = trainingLevelMapper.selectOne(wrapper);
        return trainingLevel.getTrainingId();
    }

    @Override
    public void setTid(Integer id) {
        trainingLevelMapper.setTid(id);
    }

    @Override
    public Boolean nameIsExist2(String name) {
        String trainingLevel = trainingLevelMapper.levelCheck(name);
        System.out.println("trainingLevel = " + trainingLevel);
        if (!StringUtils.isEmpty(trainingLevel)|| trainingLevel!=null) {
            return true;
        } else {
            return false;
        }
    }
}
